/*5WDelIanR8BEnstFViPZJ2wV1Htx0tfjLqzv8NcprOdbfokaxIPCVsJzWlnpCv1Q*/
/**
 * 向导批改
 * @author yinshb
 */
import React, { Component } from 'react';
import { createPage, base,ajax,toast } from 'nc-lightapp-front';
let { NCTabs, NCButton, NCModal, NCStep } = base;
import Utils from "../../../public/utils/index";
import Batcheditform from './BatchEditForm';
var EMPTY_FN = function(){};

class Batcheditmodal extends Component {
    constructor(props) {
        super(props);
        this.tableConfig = props.tableConfig;
        this.url = props.url;
        this.state = {
            showForm : false,
            modal:{
                show: false,
                modalDropup: true,
                size: 'xlg',
                fieldid:'batchupdate_modal-area'
            },
            tableItems : {},
            orgs : [],
            pks : []
        };
        this.onFinish = props.onFinish || EMPTY_FN;
    }

    show = (pks,orgs) => {
        ajax({
            url : '/nccloud/uapbd/material/queryBatchUpdateTab.do',
            data : {mdId:this.tableConfig.mdId},
            success : (res) => {
                debugger
                if(res.data && res.data.tab){
                    let meta = this.props.meta.getMeta();
                    let tableItems = {};
                    res.data.tab.forEach(element => {
                        let attrItems = [];
                        tableItems[element.code] = {
                            code : element.code,
                            name : element.name,
                            attrs : element.attrs,
                            attrItems : attrItems
                        };
                        element.attrs.forEach(attr=>{
                            this.tableConfig.tableRelation[element.code].forEach(metaid=>{
                                meta[metaid].items.forEach(item=>{
                                    if(item.attrcode === attr.code){
                                        delete item.title;
                                        let i =JSON.parse(JSON.stringify(item));//Utils.clone(item);
                                        i.visible = true;
                                        i.disabled = false;
                                        attrItems.push(i);
                                    }else if(this.tableConfig.specialItems[element.code] && this.tableConfig.specialItems[element.code] === item.attrcode){
                                        delete item.title;
                                        tableItems[element.code].special =JSON.parse(JSON.stringify(item));// Utils.clone(item);
                                        tableItems[element.code].special.visible = true;
                                        tableItems[element.code].special.disabled = false;
                                        tableItems[element.code].special.isMultiSelectedEnabled = true;
                                    }
                                });
                            });
                        });
                    });
                    this.state.tableItems = tableItems;
                    this.state.modal.show = true;
                    this.state.showForm = true;
                    this.state.orgs = orgs;
                    this.state.pks = pks;
                    this.setState(this.state);
                }
            }
        });
    }

    onsubmit = () => {
        let reqData = this.Batcheditform.getData();
        reqData.orgs = this.state.orgs;
        reqData.pks = this.state.pks;
        ajax({
            url : this.url,
            data : reqData,
            success : (res) => {
                this.state.modal.show = false;
                this.setState(this.state,()=>{this.onFinish(res);});
            }
        })
    }

    render(){

        return (
            <NCModal {...this.state.modal} fieldid = 'batchedit'>
                <NCModal.Header closeButton={false}>
                    <NCModal.Title>快速批改</NCModal.Title>
                </NCModal.Header>
                <NCModal.Body>
                    <div>{this.state.showForm && <Batcheditform tableItems={this.state.tableItems} ref={item=>this.Batcheditform=item} {...this.props} />}</div>
                </NCModal.Body>
                <NCModal.Footer>
                    <span><NCButton onClick={ this.onsubmit }>快速批改</NCButton></span>
                    <NCButton onClick={()=>{this.state.modal.show = false;this.setState(this.state);}}>关闭</NCButton>
                </NCModal.Footer>
            </NCModal>
        )
    }

}

export default Batcheditmodal;
/*5WDelIanR8BEnstFViPZJ2wV1Htx0tfjLqzv8NcprOdbfokaxIPCVsJzWlnpCv1Q*/
